<?php


namespace app\common\model\product;

use app\common\model\BaseModel;
use app\common\model\proxy\User;
use app\common\model\supplier\Supplier;
use app\shop\model\supplier\Supplier as SupplierModel;

/**
 * 商品推荐模型
 */
class Recommend extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'product_recommend_module';

    /**
     * 关联推荐商品列表
     * @return \think\model\relation\HasMany
     * @Author joey
     * @date 2024/3/20 17:01
     */
    public function recommendList()
    {
        return $this->hasMany(RecommendList::class, 'module_id');
    }

    /**
     * 代理
     * @return \think\model\relation\BelongsTo
     * @Author joey
     * @date 2024/3/29 15:22
     */
    public function proxy()
    {
        return $this->belongsTo(User::class, 'proxy_id', 'proxy_id');
    }

    public function supplier()
    {
        return $this->belongsTo(Supplier::class, 'shop_supplier_id', 'shop_supplier_id')->visible(['shop_supplier_id','name']);
    }

    /**
     * 所有
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getALL($map = [])
    {
        $model = new static;
        if ($map) {
            $model = $model->where($map);
        }
        return $model->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
    }

    /**
     * 详情
     */
    public static function detail($id)
    {
        return (new static())->find($id);
    }

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }

        $map = [];
        if (isset($where['search']) && $where['search']) {
            $map[] = ['name', 'like', '%' . trim($where['search']) . '%'];
        }


        $map2 = [];
        if($map){
            $shop_supplier_ids = (new SupplierModel())->where($map) -> column('shop_supplier_id');
            $map2[] = ['shop_supplier_id','in',$shop_supplier_ids];
        }
        if (isset($params['province_id']) && $params['province_id']) {
            $model = $model->where('province_id', '=', $params['province_id']);

        }
        if (isset($params['city_id']) && $params['city_id']) {
            $model = $model->where('city_id', '=', $params['city_id']);
        }
        if (isset($params['region_id']) && $params['region_id']) {
            $model = $model->where('region_id', '=', $params['region_id']);
        }


        unset($where['search'],$where['province_id'],$where['city_id'],$where['region_id']);
        $model = self::assWhere($model, $where);
        $list  = $model -> where($map2) -> paginate($params);

        return $list;
    }

    public static function createData($where)
    {
        $where           = self::disposeWhere($where);
        $model           = new static();
        $where['app_id'] = self::$app_id;
        $data            = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $where = self::disposeWhere($where);
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }

    /**
     * 待审核数量
     * getAuditCount
     * int
     * @return int
     * @throws \think\db\exception\DbException
     * static
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/5/20 10:35
     */
    public static function getAuditCount(){
        $map = [];
        $map[] = ['audit_status','=',0];
        $map[] = ['status','=',1];
        return self::where($map) -> count();

    }
}
